package com.easychat.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.easychat.common.LeaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author 10 2024/10/20
 */
public class JsonUtils {
    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
    public static SerializerFeature[] FEATURES = new SerializerFeature[] {SerializerFeature.WriteMapNullValue};
    public static String convertObj2Json(Object object){
        return JSON.toJSONString(object,FEATURES);
    }
    public static <T> T convertJson2Obj(String json,Class<T> classz){
        try{
            return JSONObject.parseObject(json,classz);
        }catch (Exception e){
            logger.error("convertJson2Obj异常，json:{}",json);
            throw new LeaseException("json转换对象异常");
        }
    }

    public static <T> List<T> convertJsonArray2List(String json, Class<T> classz){
        try{
            return JSONArray.parseArray(json,classz);
        }catch (Exception e){
            logger.error("convertJsonArray2List,json:{}",json,e);
            throw new LeaseException("json转换list异常");
        }
    }

}
